﻿/*
 * Copyright 2008 Pimm Hogeling
 * 
 * This file is part of Bomberleague. Contributions to this file were made
 * by (feel free to add your name and/or e-mail to this list if you believe
 * you should be mentioned here):
 * Pimm Hogeling <pimmhogeling@gmail.com>
 * 
 * Bomberleague is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * Bomberleague is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Bomberleague. If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bomberleague.language {
	public class LanguageManager {
		[Embed(source = "/../embed/languages/english.xml", mimeType = "application/octet-stream")]
		private static const ENGLISH:Class;
		private static const LANGUAGES:Array = new Array(ENGLISH);
		private static var _languageID:uint;
		private static var _languageList:Vector.<String> = new Vector.<String>();
		private static var xml:XML = XML(new ENGLISH());
		/**
		 * Returns the name if the input in the current language.
		 * 
		 * @param input             <uint> The input of which you want to know the name.
		 * @return                  <String> The name that belongs to the input.
		 */
		public static function getInputName(input:uint):String {
			if (!input) {
				return getWord("noinput");
			}
			// This way works slightly faster than: return xml.inputs.i.(@id == input).toString();
			const inputs:XMLList = xml.inputs.i;
			const m:String = input.toString();
			const l:uint = inputs.length();
			for (var i:uint; i < l; i++) {
				if (inputs[i].@id.toString() === m) {
					return inputs[i].toString();
				}
			}
			return getWord("undefinedinput");
		}
		/**
		 * Returns a word in the current language from the wordlist.
		 * 
		 * @param id                <String> The id of the word that should be returned, this is the same as the name of the node
		 *                                    in the XML file.
		 * @return                  <String> The word that belongs to the id.
		 */
		public static function getWord(id:String):String {
			return xml.wordlist[id].toString();
		}
		/**
		 * The ID of the language that is selected.
		 */
		public static function get languageID():uint {
			return _languageID;
		}
		public static function set languageID(value:uint):void {
			_languageID = value;
			xml = XML(new LANGUAGES[value]());
		}
		/**
		 * A Vector.<String> object containing the names of all languages.
		 */
		public static function get languageList():Vector.<String> {
			if (_languageList.length) {
				return _languageList;
			}
			const l:uint = LANGUAGES.length;
			for (var i:uint; i < l; i++) {
				_languageList.push(XML(new LANGUAGES[i]()).@name);
			}
			return _languageList;
		}
	}
}